import {getUserInfo, logout} from "../../api/user"
import {removeToken} from "@/utils/token"

export default {
    data() {
        return {
            userInfo: {},
            isLogin: false,
            input3: "",
            activeIndex: "0",
            imgUrl: process.env.VUE_APP_WAREHOUSE_API,
            isDisabled:false
        }
    },
    created() {
        if (this.$route.path.indexOf('user') !== -1) {
            this.isDisabled = true
        }else{
            this.isDisabled = false
        }
    },
    mounted() {
        this.getLoginUserInfo()
    },
    methods: {
        //登录
        goLogin() {
            location.href = process.env.VUE_APP_LOGIN_URL
            localStorage.setItem("isRoute", "false")
        },

        // 注册
        goRegister() {
            this.$router.push({
                path: '/register'
            })
        },
        //获取用户信息
        getLoginUserInfo() {
            getUserInfo()
                .then(res => {
                    if (res.data.code === 200) {
                        this.isLogin = true
                        this.userInfo = JSON.parse(res.data.result)
                    } else {
                        this.$message({
                            message: res.data.msg,
                            type: "warning"
                        })
                    }
                })
                .catch(() => {
                    // this.$message.error("获取用户信息失败！")
                })
        },

        errorHandler() {
            return true
        },

        handleSelect(key, keyPath) {

        },

        handleCommand(command) {
            // console.log(this.$store.getters)
            if (command === "toAdmin") {
                // let jumpurl =
                //   "http://192.168.0.134:9529/#/login?ACCESSTOKEN=" +
                //   this.$store.getters.token
                let jumpurl =
                    "http://192.168.0.134:9529/#/login?ACCESSTOKEN=" +
                    this.$store.getters.token
                window.open(jumpurl)
            } else if (command === "logout") {
                logout().then(res => {
                    if (res.data.code === 200) {
                        this.isLogin = false
                        this.$store.dispatch("user/logout").then(response => {
                            removeToken()
                            this.$router.push({
                                path: '/'
                            })
                            if (this.$route.name === 'home') {
                                location.reload()
                            }
                            // else{
                            //             this.$router.push({
                            //                 path: '/'
                            //             })
                            //         }

                        })
                    }
                }).catch(err => {
                    console.log('退出失败')
                })

            } else {
                this.$router.push({
                    path: command
                })
            }
        },
        _clickToAdmin() {
            console.log(this.$store.getters)
            //   this.$router.push({
            //     path: "http://192.168.0.134:9529/login",
            //     query: {
            // 	ACCESSTOKEN:
            // }
            //   })
        }
    }
}
